package com.sylar.happyreader;

import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.sylar.happyreader.tools.Config;
import com.sylar.happyreader.tools.Utils;
import com.sylar.happyreader.view.PageWidget;

public class ReadBookActivity extends Activity implements OnTouchListener {

	public PageWidget mPageWidget;

	TextView contentText, titleText;
	LinearLayout readlayout, readlayout1;
	LinearLayout parentView;
	int width, height;
	public static InputStream is = null;
	public static String msg = null;
	public static Handler readBookHandler;
	
	private String sex;
	private String style;
	private String bookName;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		float desity = this.getResources().getDisplayMetrics().density;
		height = Config.screenHeight;
		width = Config.screenWidth;
		
		bookName = this.getIntent().getStringExtra("bookName");
		style = this.getIntent().getStringExtra("style");
		sex = this.getIntent().getStringExtra("sex");
		
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		
		BookApplication myApp = (BookApplication) getApplication();
		
		try {
			if (ReadBookActivity.msg != null && !"".equals(ReadBookActivity.msg)) {
				if (BookPagesActivity.dbCrud != null){
					BookPagesActivity.dbCrud.insert(bookName.trim(), Config.nowPage.trim());
					BookPagesActivity.dbCrud.insertBookHisory(bookName.trim(), sex, style);
				}
					
				parentView = new LinearLayout(this);
				parentView.setLayoutParams(params);
				parentView.setOrientation(LinearLayout.VERTICAL);
				
				setContentView(parentView);

				mPageWidget = new PageWidget(this, width, height,sex,style,bookName,myApp.getmCurPageBitmap(), myApp.getmNextPageBitmap());
				mPageWidget.setOnTouchListener(this);

				parentView.addView(mPageWidget,params);

				float lightf = Utils.getSettingsparamsByFloat(this, "screenLight");
				WindowManager.LayoutParams lp = getWindow().getAttributes();
				lp.screenBrightness = lightf;
				getWindow().setAttributes(lp);
				
				Toast.makeText(ReadBookActivity.this, "提示:轻触左右下角就可以进行翻页面哦^_^", 1).show();
				
			} else {
				Toast.makeText(ReadBookActivity.this, "加载失败了哦，请重试", 1).show();
				finish();
			}
		} catch (OutOfMemoryError e) {
			e.printStackTrace();
			System.gc();
			Toast.makeText(ReadBookActivity.this, "加载失败了哦，请重试", 1).show();
			this.finish();
		}
		
//		initAirAD(this);
		
		
	}

	@Override
	protected void onPause() {
		super.onPause();
//		finishTask();
//		MobclickAgent.onPause(this);
	}

	@Override
	protected void onResume() {
//		MobclickAgent.onResume(this);
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// if (ad != null)
		// ad.destory();
		// Log.d("yixs", " onDestroy Config.pageNo" + Config.pageNo);
		// Utils.setSettingsparamsByInt(this, Config.bookName, Config.pageNo);
		if (readBookHandler != null)
			readBookHandler.sendMessage(readBookHandler.obtainMessage(BookPagesActivity.MSG_GET_BOOK_DATA));
		if (mPageWidget != null) {
			mPageWidget.destroyDrawingCache();
			mPageWidget = null;
		}
//		if(kedoutimer!=null)kedoutimer.cancel();
		super.onDestroy();

	}

	public boolean onTouch(View v, MotionEvent e) {
		boolean ret = false;
		if (v == mPageWidget) {
			ret = mPageWidget.doTouchEvent(e);
			return ret;
		}
		return false;
	}


}
